package mbp.gui.frames;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;

import mbp.agents.MbpStorageAgent;
import mbp.base.GuiFunction;
import mbp.core.Resource;
import mbp.gui.panels.ResourceListPanel;
import mbp.message.resource;

import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.BorderLayout;

public class ResourceStorageGui extends JFrame implements ActionListener
{

	private MbpStorageAgent myAgent;
	private JPanel contentPane;
	private ResourceListPanel rcVieverPanel;
	private JButton btnDelete;



	/**
	 * Create the frame.
	 */
	public ResourceStorageGui(MbpStorageAgent a) {
		super(a.getLocalName());
		myAgent = a;
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 460, 428);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		btnDelete = new JButton("Delete");
		btnDelete.setBounds(22, 360, 100, 25);
		contentPane.add(btnDelete);
		btnDelete.addActionListener(this);
		
		//Utworzenie panelu oraz nadpisanie metody podwojne klikniecie na wiersz
		rcVieverPanel = new ResourceListPanel() {
			public void processDoubleClick(int index)
			{
				getDetails(index);
			}
		};
		rcVieverPanel.setBounds(12, 12, 431, 337);
		rcVieverPanel.setBorder(new TitledBorder(null, "Resources", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(rcVieverPanel);

	}
	
	//public void AddResource(Resource rc)
	//{
	//	int i = RCmodel.getRowCount();
	//	RCmodel.insertRow(0, new Object[]{rc.GetUUID(), rc.GetType().GetResourceTypeName(), rc.GetOwner()});
	//}
	
	public void reloadResources(List<resource> rciList)
	{
		if(myAgent == null)return;
		rcVieverPanel.reloadListInfo(rciList);
	}
	
	public void getDetails(int selected)
	{
		if(selected==-1)return;
		
		String uuid = (String) rcVieverPanel.getTable().getValueAt(selected, 2);
		
		Resource rc = myAgent.getResource(uuid);
		if(rc != null)
		{
			ResourceEditor resourceEditor = new ResourceEditor(rc, GuiFunction.EDITOR, rcVieverPanel);
			resourceEditor.setVisible(true);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnDelete)
		{
			int selected = rcVieverPanel.getSelectedRow();
			if(selected==-1)return;
			
			int n = JOptionPane.showConfirmDialog(
				    this,
				    "Are you sure you want to remove the Resource from the store ?",
				    "Delete Resource",
				    JOptionPane.YES_NO_OPTION);
			
			if(n==0)
			{
				selected = rcVieverPanel.getSelectedRow();
				if(selected==-1)return;
				
				String uuid = (String) rcVieverPanel.getTable().getValueAt(selected, 2);
				
				myAgent.deleteResource(uuid);
			}
		}
	}
}
